<template>
  <com-header></com-header>
  <div class="container-fluid">
    <chart-filter-toolbar :sort="data.form.sort" :type="data.form.type"
      @setForm="setForm"></chart-filter-toolbar>
  
    <div class="chart-list">
      <div v-for="(item, index) in data.list" :key="index" class="item">
        <router-link class="btn"
          :to="{
            path: '/editor',
            query: {id: item.cid}
          }"
          target="_blank">
          <img class="img" :src="_this.$api.image(item.cid)"/>
          <div class="title">{{item.title}}</div>
        </router-link>
        <div class="chart-info">
          <div class="author">
            <img src="@/assets/image/mix/head.jpg"/>
            <span>{{item.userName}}</span>
          </div>
          <div class="statistics">
            <div class="it" title="浏览量">
              <i class="icon iconfont icon-yanjing"></i>
              <span>{{item.viewCount}}</span>
            </div>
            <div class="it" title="赞赏数">
              <i class="icon iconfont icon-collection"></i>
              <span>{{item.starCount}}</span>
            </div>
          </div>
        </div>
      </div>

    </div>
  </div>

</template>
<script setup>
  import {reactive, onMounted, onBeforeUnmount, getCurrentInstance} from "vue";
  
  // 引入组件
  import comHeader from "@/components/com-header.vue";
  import chartFilterToolbar from "./modules/chart-filter-toolbar.vue";
  

  // 获取全局的this指向
  const instance = getCurrentInstance();
  const _this = instance.appContext.config.globalProperties;

  let data = reactive({
    form: {
      sort: 'starCount',
      type: [],
      order: 'desc',
      pageNo: 0,
      pageSize: 30
    },
    loading: false, // 是否正在加载中
    list: []
  })

  // 实例挂载完成
  onMounted(() => {
    window.addEventListener('scroll', handleScroll, true);
    search();
  });

  // 卸载前
  onBeforeUnmount(() => {
    window.removeEventListener('scroll', handleScroll);
  });

  // 初始化获取数据
  function search () {
    getData();
  }

  // 获取列表
  async function getData (pageNo = 0) {
    data.form.pageNo = pageNo;
    let param = data.form;
    data.loading = true;
    let res = await _this.$api.chartList(param);
    data.loading = false;
    data.list = pageNo == 0 ? res.data : [...data.list, ...res.data];
  }
  
  // 设置表单值
  function setForm (key, value) {
    data.form[key] = value;
    search();
  }

  // 滚动到底部，加载更多
  function handleScroll (e) {
    let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    let clientHeight = document.documentElement.clientHeight;
    let scrollHeight = document.documentElement.scrollHeight;
    let bottomOfWindow = scrollTop + clientHeight >= scrollHeight - 50; // 是否滚动到最底部
    console.log(bottomOfWindow)

    if (scrollTop != 0 && bottomOfWindow && data.loading == false) {
      getData(data.form.pageNo + 1);
    }
  }


</script>
<style lang="scss" scoped>
.container-fluid {
  padding: 0 40px;

  .chart-list {
    display: flex;
    flex-wrap: wrap;
    .item {
      flex: 0 0 20%;
      padding: 0 15px 30px;
      box-sizing: border-box;
      .btn {
        position: relative;
        margin-bottom: 16px;
        padding-top: 75%;
        width: 100%;
        height: 0;
        border-radius: 7px;
        box-shadow: 0 0 20px rgba(0,0,0,0.1);
        overflow: hidden;
        .img {
          position: absolute;
          z-index: 1;
          top: 0;
          left: 0;
          width: 100%;
        }
        .title {
          opacity: 0;
          position: absolute;
          z-index: 2;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          display: flex;
          justify-content: center;
          align-items: center;
          padding: 10px 20px;
          color: #fff;
          font-size: 20px;
          text-align: center;
          background: rgba(0,0,0,0.4);
          transition: opacity .3s;
        }
        &:hover .title {
          opacity: 1;
        }
      }
      .chart-info {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
        .author {
          display: flex;
          align-items: center;
          width: 33%;
          cursor: pointer;
          overflow: hidden;
          white-space: nowrap;
          img {
            margin-right: 5px;
            width: 20px;
            height: 20px;
            border-radius: 50%;
          }
          &:hover {
            span {
              text-decoration: underline;
            }
          }
        }
        .statistics {
          display: flex;
          flex-direction: row-reverse;
          width: 66%;
          color: #999;
          .it {
            margin-left: 10px;
            i {
              margin-right: 3px;
            }
            span {
              position: relative;
              top: -1px;
            }
          }
        }
      }
    }
  }
}
@media (max-width: 1700px) {
  .container-fluid .chart-list .item {
    flex: 0 0 25%;
  }
}
@media (max-width: 1200px) {
  .container-fluid .chart-list .item {
    flex: 0 0 33.333333%;
  }
}
@media (max-width: 992px) {
  .container-fluid .chart-list .item {
    flex: 0 0 50%;
  }
}
</style>